import random
import ConfigSpace as CS
import ConfigSpace.hyperparameters as CSH
import itertools

class Cast():
    cs_attributes = CS.ConfigurationSpace()
    attribute_list = ['to']
    all_attributes = []

    def __init__(self):
        print('Cast Init--------------------------')
        self.init_all_attributes()
        self.init_cs()

    def init_all_attributes(self):
        for i in range(1, len(Cast.attribute_list)+1) :
            #此处取所有组合的可能值，存在列表里
            for attribute in itertools.combinations(Cast.attribute_list, i):
                l = list(attribute)
                Cast.all_attributes.append(l)

        #print('all_attributes: ', all_attributes)
        print('all attributes:')
        for a in Cast.all_attributes:
            print(a)

    def init_cs(self):
        length = len(Cast.all_attributes)
        attributes = CSH.UniformIntegerHyperparameter(name='combination', lower=0, upper=length)
        Cast.cs_attributes.add_hyperparameter(attributes)

    def get_random_combination(self):
        s = Cast.cs_attributes.sample_configuration()
        index = s.get_dictionary()['combination']
        print('get combination: ', index)

        length = len(Cast.all_attributes)

        if index >= length:
            index = length - 1

        return Cast.all_attributes[index]       

    def get_max_attributes_combination(self):
        return len(Cast.all_attributes)

    def get_attributes_by_index_or_random(self, shapes, index):

        print('get_attributes_by_index_or_random, index: ', index)    
            
        attributes = {}
        shape_out = []

        print('Cast, shapes:', shapes[0])

        if index != -1:
            attr_combination = Cast.all_attributes[index]
        else:
            attr_combination = self.get_random_combination()

        if 'to' in attr_combination:
            to = random.randint(1, 13)
            attributes['to'] = to

        print('------ attributes:', attributes, 'shape_out:', shape_out)

        return attributes, shape_out

    def input_shape_correct(self, shapes):
        return True    

